import router from '@/router'
import store from '@/store'

import asyncRoute from '@/router/routes/asyncRoute'

router.beforeEach((to,from,next) => {
    if (!router.options.routes[0].children) {
        // 角色 
        let role = store.getters.role
        if (role != '') {
          // 得到的是一个子路由
          let routes = asyncRoute.filter(item => item.meta.role.includes(role))      
          let adminRoute = router.options.routes[0]
          adminRoute.children = routes
          // 动态添加路由
          router.addRoute(adminRoute)
          // 重定向
          next({ ...to })
        } else {
          next()
        }
      } else {
        next()
      }
})